package com.mainworks.commons;

import com.mainworks.commons.enums.ResponseCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class UnifyResponse implements Serializable {

    /**
     * 成功
     */

    public static final int STATUS_SUCCESS = 200;
    /**
     * 失败
     */
    public static final int STATUS_FAIL = 400;

    /**
     * 没有登录
     */
    public static final int STATUS_NO_LOGIN = 401;

    /**
     *  无权,或越权
     */
    public static final int STATUS_UITra_VIRES = 403;

    /**
     *  异地登录或是过期
     */
    public static final int STATUS_OVERDUE = 405;

    /**
     *  异常
     */
    public static final int STATUS_ERROR = 500;
    /**
     * 状态码
     */
    private  int code;
    /**
     * 信息
     */
    private String msg;
    /**
     * 消息体
     */
    private Object data;

    private UnifyResponse() {
    }

    public static UnifyResponse success(String msg,Object object){
        UnifyResponse unifyResponse=new UnifyResponse();
        unifyResponse.setCode(STATUS_SUCCESS);
        unifyResponse.setMsg(msg);
        unifyResponse.setData(object);
        return unifyResponse;
    }
    public static UnifyResponse success(String msg){
        return success(msg,null);
    }
    public static UnifyResponse success(Object object){
        return success("success",object);
    }
    public static UnifyResponse success(){
        return success("操作成功",null);
    }

    public static UnifyResponse error(ResponseCode code, String msg){
        int status;
        switch (code){
            case FAIL :
                status = STATUS_FAIL;
                break;
            case NO_LOGIN:
                status = STATUS_NO_LOGIN;
                break;
            case UITra_VIRES:
                status = STATUS_UITra_VIRES;
                break;
            case OVERDUE:
                status = STATUS_OVERDUE;
                break;
            default: status = STATUS_ERROR;
                break;

        }
        UnifyResponse unifyResponse=new UnifyResponse();
        unifyResponse.setCode(status);
        unifyResponse.setMsg(msg);
        unifyResponse.setData(null);
        return unifyResponse;
    }

    public static UnifyResponse error(String msg){
        return error(ResponseCode.ERROR,msg);
    }

}
